package test;

import java.util.Scanner;

public class FourhTransIPv4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入IPv4地址");
        String str = sc.next();
        boolean check = check(str);
        if(!check){
            System.out.println("invalid IP");
        }else{
            long method = getMethod(str);
            System.out.println(method);
        }
    }
    public static boolean check(String str) {
        String[] split = str.split("#");
        if (split.length != 4) {
            return false;
        } else {
            int i = Integer.parseInt(split[1]);
            if (i < 1 && i > 128) {
                return false;
            } else {
                for (i = 1; i <= 3; i++) {
                    int m = Integer.parseInt(split[i]);
                    if (m < 0 || m > 255) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    public static long getMethod(String str) {
        StringBuffer sb=new StringBuffer();
        String[] split = str.split("#");
        for(int i=0;i<4;i++){
            int m = Integer.parseInt(split[i]);
            String s = Integer.toString(m, 2);
            int a=s.length();
            if(a<8){
                for(int j=1;j<=8-a;j++){
                    sb.append("0");
                }
            }
        sb.append(s);

        }
        System.out.println(sb.toString());
        long number = Long.parseLong(sb.toString(), 2);
        return number;
    }
}


